package com.campus.service.Impl;

import com.campus.dao.NoticeDao;
import com.campus.pojo.Echarts;
import com.campus.pojo.Notice;
import com.campus.service.NoticeService;
import com.campus.tools.CommonTool;
import com.campus.tools.UUIDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
@Service
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    NoticeDao dao;
    @Override
    public int count(Map<String, Object> params) {
        return dao.count(params);
    }

    @Override
    public List<Notice> selectByPage(Map<String, Object> params) {
        return dao.selectByPage(params);
    }

    @Override
    public List<Notice> getNoticeById(String id) {
        return dao.getNoticeById(id);
    }

    @Override
    public boolean addNotice(Notice notice, String answerids, String uid) {
        // 添加notice
        String nid = UUIDGenerator.getUUID();
        notice.setId(nid);
        notice.setUserId(uid);
        notice.setOperateTime(CommonTool.getNowDateStr());
        int resultNotice = dao.addNotice(notice);
        // 添加answer
        String[] ids = answerids.split(",");
        int count = 0;
        for (int i = 0; i < ids.length; i++) {
            count += dao.addAnswer(UUIDGenerator.getUUID(), nid, ids[i]);
        }
        return (resultNotice > 0) && (count > 0);
    }

    @Override
    public boolean delNotice(String ids) {
        ids = "'" + ids + "'";
        int result1 = dao.delAnswer(ids);
        int result2 = dao.delNotice(ids);
        return (result1 > 0) && (result2 > 0);
    }

    @Override
    public List<Echarts> getNoticeDetailNum(String nid) {
        return dao.getNoticeReadNum(nid);
    }

    @Override
    public List<Echarts> getNoticeVoteNum(String nid) {
        return dao.getNoticeVoteNum(nid);
    }
}
